package dear.dbb.pic;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class FavActivity extends Activity implements Callback, OnTouchListener {

	private SurfaceView sv;
	private SurfaceHolder sh;
	private Canvas cv;
	private int width, height;
	private Timer timer;
	private int colorIndex = 0;
	boolean mbloop = false;
	private final Vector<Point> lst = new Vector();

	float oldx = 0;
	float oldy = 0;
	private Bitmap memBm;
	private final Object run = new String();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		sv = new SurfaceView(this);
		sv.setOnTouchListener(this);
		setContentView(sv);
		Toast.makeText(this, "试着用手指点一下，有意外哦", 1).show();
		sh = sv.getHolder();
		sh.addCallback(this);
		width = getWindowManager().getDefaultDisplay().getWidth();
		height = getWindowManager().getDefaultDisplay().getHeight();
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		timer = new Timer();
		mbloop = true;
		// runHeart();
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				if (mbloop) {
					synchronized (run) {
						int offset = 3;
						Iterator<Point> it = lst.iterator();
						while (it.hasNext()) {
							Point pl = it.next();
							pl.y += offset;
							pl.x += new Random().nextInt() > 5 ? offset : -offset;
							if (pl.y >= height || pl.x < 0 || pl.x > width) {
								it.remove();
							}
						}
						runHeart();
					}
				}
			}
		}, 0, 100);
	}

	/**
	 * 运行时画图
	 */
	private void runHeart() {
		memBm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
		colorIndex++;
		int index = colorIndex / 5;
		int[] heartColors = new int[] { Color.YELLOW, Color.RED, Color.GREEN, Color.BLUE };
		// cv = sh.lockCanvas(new Rect(0, 0, width, height));
		cv = new Canvas(memBm);
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		cv.drawColor(Color.GRAY);
		paint.setColor(heartColors[index % 4]);
		int i, j;
		float x, y;
		double r;
		for (i = 0; i <= 90; i++) {
			for (j = 0; j <= 90; j++) {
				r = Math.PI / 45 * i * (1 - Math.sin(Math.PI / 45 * j)) * width / 16;
				x = Math.round(r * Math.cos(Math.PI / 45 * j) * Math.sin(Math.PI / 45 * i) + width / 2);
				y = Math.round(r * Math.sin(Math.PI / 45 * j) + height * 2 / 3);
				// if (oldx == 0) {
				cv.drawPoint(x, y, paint);
				// } else {
				// cv.drawLine(oldx, oldy, x, y, paint);
				// }
				// oldx = x;
				// oldy = y;
			}
		}
		// 写题头
		paint.setTextSize(32);
		String text = "to 最亲爱的大宝宝";
		RectF rect = new RectF(0, 0, width, 100);
		paint.setColor(0xffffcccc);
		cv.drawRoundRect(rect, (float) 1.0, (float) 1.0, paint);
		paint.setColor(Color.RED);
		cv.drawText(text.substring(0, index % text.length() + 1), width / 2 - (float) (text.length() / 2) * 32,
				rect.height() / 2 + 16, paint);
		// 写结尾
		text = "让我们相爱一生一世";
		rect = new RectF(0, height - 100, width, height);
		paint.setColor(0xffffcccc);
		// cv.drawRect(rect, paint);
		cv.drawRoundRect(rect, (float) 1.0, (float) 1.0, paint);
		paint.setColor(Color.RED);
		cv.drawText(text.substring(0, index % text.length() + 1), width / 2 - (float) (text.length() / 2) * 32, height
				- rect.height() / 2 - 16, paint);
		// 画玫瑰
		paint.setColor(Color.RED);
		List<Point> ls = new ArrayList();
		ls.addAll(lst);
		for (Point p : ls) {
			// for (int t = 0; t <= 90; t++) {
			// double angle = Math.PI / 45 * t;
			// double radius = 20 * Math.sin(2 * angle);
			// x = Math.round(radius * Math.cos(angle)) + p.x;
			// y = Math.round(radius * Math.sin(angle)) + p.y;
			// cv.drawPoint(x, y, paint);
			// }
			// cv.drawArc(new RectF(p.x - 10, p.y - 5, p.x, p.y + 5), 180, 180,
			// true, paint);
			// cv.drawArc(new RectF(p.x, p.y - 5, p.x + 10, p.y + 5), 180, 180,
			// true, paint);
			Path mPath = new Path();
			mPath.moveTo(p.x - 10, p.y);
			mPath.lineTo(p.x, p.y + 10);
			mPath.lineTo(p.x + 10, p.y);
			mPath.addArc(new RectF(p.x - 10, p.y - 5, p.x, p.y + 5), 180, 180);
			mPath.addArc(new RectF(p.x, p.y - 5, p.x + 10, p.y + 5), 180, 180);
			mPath.close();
			cv.drawPath(mPath, paint);
		}

		Bitmap bmp = readBitmap(getResources(), R.drawable.dbb);
		Matrix matrix = new Matrix();
		matrix.setScale(0.25f, 0.25f);
		matrix.postTranslate(width / 2 - bmp.getWidth() / 8, height / 2 - bmp.getHeight() / 8 + 80);
		cv.drawBitmap(bmp, matrix, paint);
		Canvas render = sh.lockCanvas();
		if (memBm != null && render != null) {
			render.drawBitmap(memBm, 0, 0, null);
			sh.unlockCanvasAndPost(render);
			memBm.recycle();
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		mbloop = false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			synchronized (run) {
				Point p = new Point((int) event.getX(), (int) event.getY());
				lst.addElement(p);
			}
		}
		return false;
	}

	/**
	 * 读取图片
	 * 
	 * @param r
	 *            resources
	 * @param resId
	 *            资源id
	 * @return
	 */
	public static Bitmap readBitmap(Resources r, int resId) {
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.RGB_565;
		opt.inPurgeable = true;
		opt.inInputShareable = true;
		InputStream is = r.openRawResource(resId);
		return BitmapFactory.decodeStream(is, null, opt);
	}

}